﻿package mortal.game.view.pack.other
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.tools.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.utils.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.firstReCharge.*;
    import mortal.mvc.interfaces.*;

    public class ZuandanPackageWin extends SmallWindow
    {
        private var _itemData:ItemData;
        private var _bg:GImageBitmap;
        private var _rechargeBtn:GLoadingButton;
        private var _getGiftBtn:GLoadingButton;
        private var _txt:GTextFiled;
        private var _primeCost:GTextFiled;
        private var _nowCost:GTextFiled;
        private var _diamondIcon:GBitmap;
        private var _diamondNum:GLabel;
        private var _discountTagVec:Vector.<GBitmap>;
        private const _discountArr:Array;
        private var _timeTxt:GTextFiled;
        private var _curDiscount:Number;
        private var _rewardItem:EffectItem;
        private var _curClassItem:EffectItem;
        private var _nextClassItem:EffectItem;
        private var _arrow:GBitmap;
        private var _vSplitLine:ScaleBitmap;
        private var _primeCostTxt:GTextFiled;
        private var _nowCostTxt:GTextFiled;
        private var _desc:GTextFiled;
        private var _deleteLine:GBitmap;
        private static var _instance:ZuandanPackageWin;

        public function ZuandanPackageWin(param1:ILayer = null)
        {
            this._discountArr = [4.4, 5.1, 5.2, 5.4];
            if (_instance != null)
            {
                throw new Error(" ZuandanPackage 单例 ");
            }
            setSize(324, 280);
            this.title = Language.getString(35472);
            return;
        }// end function

        private function setEndTimeTxt() : void
        {
            var _loc_2:Date = null;
            if (!ItemsUtil.isPrescriptionItem(this._itemData as ItemData))
            {
                this._timeTxt.htmlText = "";
                return;
            }
            var _loc_1:* = ClockManager.instance.nowDate;
            if (this._itemData.serverData)
            {
                if (this._itemData.extInfo && this._itemData.extInfo.app_dt)
                {
                    _loc_2 = DateParser.strToDateNormal(this._itemData.extInfo.app_dt);
                }
                else if (this._itemData.serverData.createDt)
                {
                    _loc_2 = this._itemData.serverData.createDt;
                }
                else
                {
                    _loc_2 = _loc_1;
                }
            }
            else
            {
                _loc_2 = _loc_1;
            }
            var _loc_3:String = "";
            var _loc_4:* = new Date(_loc_2.fullYear, _loc_2.month, _loc_2.date + this._itemData.itemInfo.prescription, _loc_1.hours, _loc_1.seconds, _loc_1.minutes);
            if (_loc_2)
            {
                if (!ItemsUtil.isPrescriOutTime(this._itemData as ItemData))
                {
                    _loc_3 = Language.getStringByParam(20758, DateParser.parse(_loc_4, "yyyy年MM月dd日hh:mm:ss"));
                }
                else
                {
                    _loc_3 = Language.getString(35473);
                }
            }
            else
            {
                _loc_3 = Language.getStringByParam(20758, DateParser.parse(_loc_4, "yyyy年MM月dd日hh:mm:ss"));
            }
            this._timeTxt.htmlText = _loc_3;
            return;
        }// end function

        private function setTxt() : void
        {
            this._diamondNum.htmlText = String(MoneyUtil.getCoinHtml(Cache.instance.role.money.gold));
            this._primeCost.text = String(this._itemData.itemInfo.effectEx);
            this._nowCost.text = String(this._itemData.itemInfo.effect);
            this.setEndTimeTxt();
            return;
        }// end function

        private function setDisCount() : void
        {
            this._curDiscount = int(this._itemData.itemInfo.effect * 100 / this._itemData.itemInfo.effectEx + 0.5) / 10;
            var _loc_1:int = 0;
            while (_loc_1 < this._discountArr.length)
            {
                
                this._discountTagVec[_loc_1].visible = false;
                if (this._curDiscount == this._discountArr[_loc_1])
                {
                    this._discountTagVec[_loc_1].visible = true;
                }
                _loc_1++;
            }
            return;
        }// end function

        public function set itemData(param1:ItemData) : void
        {
            this._itemData = param1;
            this.setDisCount();
            this.setTxt();
            this.clearItems(true);
            this.createReward();
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.ZuandanPackageBg, 28, 33, this);
            this._txt = UIFactory.gTextField("", 20, 160, 300, 200, this, GlobalStyle.textFormatHuang2);
            this.pushUIToDisposeVec(UIFactory.bg(0, 146, 320, 10, this, ImagesConst.ShopGroupTabBg));
            this._vSplitLine = UIFactory.bg(120, 36, 2, 114, this, ImagesConst.VSplitLine);
            this.pushUIToDisposeVec(UIFactory.label(Language.getString(35474), 40, 240, 100, 25, TextFormatAlign.LEFT, this, false, GlobalStyle.colorHuangUint));
            this.pushUIToDisposeVec(UIFactory.bg(130, 240, 160, 20, this, ImagesConst.PanelBg));
            this._diamondIcon = UIFactory.gBitmap(ImagesConst.Yuanbao, 268, 242, this);
            this._diamondNum = UIFactory.label("0", 168, 242, 100, 25, TextFormatAlign.RIGHT, this);
            this._primeCostTxt = UIFactory.gTextField(Language.getString(35475), 220, 50, 200, 50, this, GlobalStyle.textFormatHong.setSize(16));
            this._nowCostTxt = UIFactory.gTextField(Language.getString(35476), 220, 80, 200, 50, this, GlobalStyle.textFormatHuang2.setSize(16));
            var _loc_1:* = GlobalStyle.textFormatHong.setSize(16);
            _loc_1.align = TextFormatAlign.CENTER;
            var _loc_2:* = GlobalStyle.textFormatHuang2.setSize(16);
            _loc_2.align = TextFormatAlign.CENTER;
            this._primeCost = UIFactory.gTextField("", 185, 50, 200, 50, this, _loc_1);
            this._nowCost = UIFactory.gTextField("", 185, 80, 200, 50, this, _loc_2);
            this._deleteLine = UIFactory.bitmap(ImagesConst.deleteLine, 271, 58, this);
            this._desc = UIFactory.gTextField("", 137, 105, 200, 50, this, _loc_2);
            this._arrow = UIFactory.bitmap("", 64, 104, this);
            this._arrow.scaleY = -1;
            this._timeTxt = UIFactory.gTextField("", 70, 152, 220, 20, this, GlobalStyle.textFormatBai, true);
            this._rechargeBtn = UIFactory.gLoadingButton(ResFileConst.RechargeBtn, 48, 197, 114, 33, this);
            this._rechargeBtn.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._rechargeBtn.drawNow();
            this._getGiftBtn = UIFactory.gLoadingButton(ResFileConst.GiftBagBuyBtn, 170, 197, 114, 33, this);
            this._getGiftBtn.configEventListener(MouseEvent.CLICK, this.clickHandler);
            this._getGiftBtn.drawNow();
            this._discountTagVec = new Vector.<GBitmap>;
            var _loc_3:int = 0;
            while (_loc_3 < this._discountArr.length)
            {
                
                if (_loc_3 == (this._discountArr.length - 1))
                {
                    this._discountTagVec[_loc_3] = UIFactory.gBitmap("", 195, 110, this);
                }
                else
                {
                    this._discountTagVec[_loc_3] = UIFactory.gBitmap("", 225, 110, this);
                }
                _loc_3++;
            }
            LoaderHelp.addResCallBack(ResFileConst.TimeLimitGiftBag, this.onLoadComl);
            return;
        }// end function

        private function onLoadComl() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            this._arrow.bitmapData = GlobalClass.getBitmapData(ImagesConst.TimeLimitGiftBagArrow);
            var _loc_1:int = 0;
            while (_loc_1 < 4)
            {
                
                this._discountTagVec[_loc_1].bitmapData = GlobalClass.getBitmapData(ImagesConst["TimeLimitGiftBagDiscountTag" + String(_loc_1)]);
                _loc_1++;
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            LoaderHelp.removeResEvent(ResFileConst.TimeLimitGiftBag, this.onLoadComl);
            this._rechargeBtn.dispose(param1);
            this._rechargeBtn = null;
            this._desc.dispose(param1);
            this._desc = null;
            this._getGiftBtn.dispose(param1);
            this._getGiftBtn = null;
            this._txt.dispose(param1);
            this._txt = null;
            this._bg.dispose(param1);
            this._bg = null;
            this._primeCostTxt.dispose(param1);
            this._primeCostTxt = null;
            this._nowCostTxt.dispose(param1);
            this._nowCostTxt = null;
            this._primeCost.dispose(param1);
            this._primeCost = null;
            this._nowCost.dispose(param1);
            this._nowCost = null;
            this._deleteLine.dispose(param1);
            this._deleteLine = null;
            this._diamondIcon.dispose(param1);
            this._diamondIcon = null;
            this._diamondNum.dispose(param1);
            this._diamondNum = null;
            this._arrow.dispose(param1);
            this._arrow = null;
            this._vSplitLine.dispose(param1);
            this._vSplitLine = null;
            this._timeTxt.dispose(param1);
            this._timeTxt = null;
            var _loc_2:int = 0;
            while (_loc_2 < 4)
            {
                
                this._discountTagVec[_loc_2].dispose(param1);
                this._discountTagVec[_loc_2] = null;
                _loc_2++;
            }
            this.clearItems(param1);
            super.disposeImpl(param1);
            return;
        }// end function

        private function getArgItem(param1:ItemData, param2:int = 0, param3:int = 0, param4:int = 1, param5:int = 0) : EffectItem
        {
            var _loc_6:EffectItem = null;
            if (param1.itemInfo && ItemsUtil.isFitCarrer(param1))
            {
                _loc_6 = UICompomentPool.getUICompoment(EffectItem);
                _loc_6.isDragAble = false;
                _loc_6.isDropAble = false;
                _loc_6.itemData = param1;
                _loc_6.amount = param4;
                _loc_6.x = param2;
                _loc_6.y = param3;
                this.addChild(_loc_6);
                if (param5 == 0)
                {
                    _loc_6.setItemStyle(ItemStyleConst.Small, ImagesConst.RewardItemBg);
                    _loc_6.showEffect(true, -2, 18, "ArroundEffect_zhi_small.swf");
                    _loc_6.updateColorShow();
                }
                else
                {
                    _loc_6.setItemStyle(ItemStyleConst.Big, ImagesConst.RewardItemBg, 4, 4);
                    _loc_6.showEffect(true, 31, 36, "ArriundEffect_zhi_big.swf");
                    _loc_6.updateColorShow();
                }
                if (param1.itemCode == GameConst.TConstWingShapeCardFlyCode)
                {
                    this._desc.text = Language.getString(35477);
                }
                else
                {
                    this._desc.text = "";
                }
            }
            return _loc_6;
        }// end function

        private function createReward() : void
        {
            var _loc_3:Array = null;
            var _loc_4:ItemData = null;
            var _loc_1:* = this._itemData.itemInfo as ItemGiftBagInfo;
            var _loc_2:* = _loc_1.itemList1.split("&");
            var _loc_5:* = _loc_2.length;
            if (_loc_2.length == 2)
            {
                _loc_3 = _loc_2[0].split("#");
                _loc_4 = new ItemData(int(_loc_3[1]));
                this._rewardItem = this.getArgItem(_loc_4, 140, 65, int(_loc_3[2]), 1);
                _loc_3 = _loc_2[1].split("#");
                _loc_4 = new ItemData(int(_loc_3[1]));
                this._nextClassItem = this.getArgItem(_loc_4, 50, 106);
                this._curClassItem = this.getArgItem(this._itemData, 50, 46);
                this.setStlye1();
            }
            else if (_loc_2.length == 1)
            {
                _loc_3 = _loc_2[0].split("#");
                _loc_4 = new ItemData(int(_loc_3[1]));
                this._rewardItem = this.getArgItem(_loc_4, 70, 65, int(_loc_3[2]), 1);
                this.setStlye2();
            }
            return;
        }// end function

        private function setStlye1() : void
        {
            if (this._nextClassItem)
            {
                this._nextClassItem.visible = true;
            }
            if (this._curClassItem)
            {
                this._curClassItem.visible = true;
            }
            this._arrow.visible = true;
            this._vSplitLine.visible = true;
            this._primeCostTxt.x = 220;
            this._nowCostTxt.x = 220;
            this._primeCost.x = 185;
            this._nowCost.x = 185;
            this._deleteLine.x = 271;
            return;
        }// end function

        private function setStlye2() : void
        {
            if (this._nextClassItem)
            {
                this._nextClassItem.visible = false;
            }
            if (this._curClassItem)
            {
                this._curClassItem.visible = false;
            }
            this._arrow.visible = false;
            this._vSplitLine.visible = false;
            this._primeCostTxt.x = 190;
            this._nowCostTxt.x = 190;
            this._primeCost.x = 155;
            this._nowCost.x = 155;
            this._deleteLine.x = 241;
            return;
        }// end function

        private function clearItems(param1:Boolean) : void
        {
            if (this._rewardItem)
            {
                this._rewardItem.dispose(param1);
            }
            if (this._curClassItem)
            {
                this._curClassItem.dispose(param1);
            }
            if (this._nextClassItem)
            {
                this._nextClassItem.dispose(param1);
            }
            this._rewardItem = null;
            this._curClassItem = null;
            this._nextClassItem = null;
            return;
        }// end function

        private function clickHandler(event:MouseEvent) : void
        {
            var str:String;
            var e:* = event;
            if (e.target == this._rechargeBtn)
            {
                GameUtil.gotoRecharge(ModuleTypeConst.ZuandanPackageWin);
            }
            else if (e.target == this._getGiftBtn)
            {
                var enterAlert:* = function () : void
            {
                if (Cache.instance.role.enoughMoney(EPriceUnit._EPriceUnitGold, _itemData.itemInfo.effect))
                {
                    GameProxy.packProxy.useItem(_itemData.serverData.uid, _itemData.itemCode, 1);
                    ZuandanPackageWin.instance.hide();
                }
                else
                {
                    MsgManager.showRollTipsMsg(Language.getString(35469));
                }
                return;
            }// end function
            ;
                str = Language.getStringByParam(35478, this._itemData.itemInfo.effect, this._itemData.itemInfo.name);
                TodayNotTipUtil.toDayNotTip(TodayNoTipsConst.MulSelectWin, enterAlert, str, null, Alert.OK | Alert.CANCEL);
            }
            return;
        }// end function

        override protected function closeBtnClickHandler(event:MouseEvent) : void
        {
            if (GiftsUtil.currentGiftType)
            {
                GiftsUtil.notToUseGiftDic[GiftsUtil.currentGiftType] = true;
                GiftsUtil.currentGiftType = "";
            }
            super.closeBtnClickHandler(event);
            return;
        }// end function

        public static function get instance() : ZuandanPackageWin
        {
            if (_instance == null)
            {
                _instance = new ZuandanPackageWin;
                _instance.layer = LayerManager.fullScreenTopLayer;
            }
            return _instance;
        }// end function

    }
}
